﻿#region XNA Libraries

using Microsoft.Xna.Framework;

#endregion

namespace main.src.game
{

    public sealed class Game_Sphere
    {

        #region Variables

        public Vector3 Position = Vector3.Zero;
        public float   Radius      = 0;
        public Vector3 Velocity = Vector3.Zero;
        public float   Mass     = 0;

        #endregion

        #region Collision

        public bool Collided(Game_Sphere another) { return ((Position - another.Position).Length() < (Radius + another.Radius)); }

        public Vector3[] Collide(Game_Sphere another, bool apply = false)
        {
            if (Collided(another))
            {
                Vector3 x = Position - another.Position;
                x.Normalize();

                Vector3 x1 = Vector3.Multiply(x, Velocity);

                Vector3 v1x = x * x1;
                Vector3 v1y = Velocity - v1x;

                x *= -1;
                Vector3 x2 = Vector3.Multiply(x, Velocity);

                Vector3 v2x = x * x2;
                Vector3 v2y = another.Velocity - v2x;

                Vector3[] result = {v1x * (Mass - another.Mass) / (Mass + another.Mass) + v2x * (2 * another.Mass) / (Mass + another.Mass) + v1y,
                                        v1x * (2 * Mass) / (Mass + another.Mass) + v2x * (another.Mass - Mass) / (Mass + another.Mass) + v2y};

                if (apply)
                {
                    Velocity = result[0];
                    another.Velocity = result[1];
                }

                return result;
            }
            return null;
        }

        #endregion

    }

}